<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="setFormData(null,true)">
        创建权限组
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="name"
        label="权限组名称"
      />
      <el-table-column
        prop="code"
        label="权限组标识"
      />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{row}">
          <el-button type="success" @click="setFormData(row)">编辑 </el-button>
          <el-button type="info" @click="showRoleMenuList(row)">配置权限 </el-button>
<!--          <el-button type="danger">删除 </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="dataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="权限组名称" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="权限组标识" prop="code">
          <el-input v-model="fromTemp.code" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveRole">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="可访问菜单" :visible.sync="dialogButtonFormVisible" style="margin: auto;">
      <SystemMenuList :key="itemsStatus" :menuid="menuId" :roleid="roleId" />
    </el-dialog>
  </div>
</template>
<script>
import { GetSystemRoleList, SaveSystemRole } from '@/api/SystemManagerApi'
import SystemMenuList from '@/views/SystemSetting/SystemRole/components/SystemMenuList'
export default {
  components: { SystemMenuList },
  data() {
    return {
      rules: {
        name: [{ required: true, message: 'type is required', trigger: 'change' }],
        code: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑权限组',
      fromTemp: {
        name: '',
        code: '',
        id: null
      },
      roleId: '',
      dialogFormVisible: false,
      dialogButtonFormVisible: false,
      listLoading: false,
      menuId: null,
      list: [],
      itemsStatus: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetSystemRoleList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    setFormData(row, isCreate) {
      if (isCreate) {
        if (row) {
          this.resetFormData(row.id)
        } else {
          this.resetFormData()
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else {
        // eslint-disable-next-line no-undef
        this.fromTemp = Object.assign({}, row)
      }
      this.dialogFormVisible = true
    },
    resetFormData(parentId) {
      this.fromTemp = {
        name: '',
        code: '',
        id: null
      }
    },
    saveRole() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          SaveSystemRole(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    showRoleMenuList(row) {
      this.roleId = row.id
      this.dialogButtonFormVisible = false
      this.$nextTick(() => {
        this.dialogButtonFormVisible = true
        this.itemsStatus = this.uuid()
      })
    },
    uuid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
      }
      return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
    }
  }
}
</script>
